package com.zygh.policeasset.service;


import com.alibaba.fastjson.JSONArray;
import com.zygh.policeasset.pojo.RoleInfo;
import com.zygh.policeasset.pojo.User;
import com.zygh.policeasset.security.CustomUserDetails;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class JwtUserDetailsService implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private LoginAttemptService loginAttemptService;

    @Autowired
    private HttpServletRequest request;

    @Override
    public UserDetails loadUserByUsername(String value) throws UsernameNotFoundException {

        User user = userService.findByUsername(value);

        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", value));
        }

        String roleMenu = roleService.findById(user.getRoleId());

        List<RoleInfo> ts = JSONArray.parseArray(roleMenu,RoleInfo.class);

        return new CustomUserDetails(user, ts);
    }
}
